
package com.baomidou.springwind.utils;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 参数Map
 * 
 * @author: luox
 * @version: 1.0
 * @since 2017-09-10
 */
public class ParamMap<K, V> implements Map<K, V> {
	private static final Log log = LogFactory.getLog(ParamMap.class);

	private Map<K, V> map;
	
	/**
	 * 默认 clone map
	 * 
	 * @param map
	 */
	public ParamMap() {
		this.map = new HashMap<K, V>();
	}

	/**
	 * 默认 clone map
	 * 
	 * @param map
	 */
	public ParamMap(Map<K, V> map) {
		this.map = map;
	}

	/**
	 * 删除Key
	 * 
	 * @param keys
	 * @return
	 */
	public Object[] removes(Object[] keys) {
		List<Object> removedKeys = new ArrayList<Object>();
		for (int i = 0; i < keys.length; i++) {
			Object key = keys[i];
			if (this.map.containsKey(key)) {
				removedKeys.add(this.map.remove(key));
			}
		}
		return removedKeys.toArray();
	}

	/**
	 * 转换为HashMap
	 * 
	 * @return
	 */
	public HashMap<K, V> toHashMap() {
		return new HashMap<K, V>(this.map);
	}

	/**
	 * 获取字符串
	 * 
	 * @param key
	 * @return
	 */
	public String getString(Object key) {
		Object value = map.get(key);
		return StringUtils.asString(value);
	}

	/**
	 * 获取字符串
	 * 
	 * @param key
	 * @param defValue
	 * @return
	 */
	public String getString(Object key, String defValue) {
		String value = getString(key);
		if (StringUtils.isValid(value)) {
			return value;
		}
		return defValue;
	}

	/**
	 * 判断数据有效性 object != null && object.toString().length() > 0
	 * 
	 * @param key
	 * @return
	 */
	public boolean isValid(Object key) {
		return getString(key).length() > 0;
	}

	/**
	 * 判断多个数据的有效性
	 * 
	 * @param key
	 * @return
	 */
	public boolean isValid(String[] keys) {
		for (int i = 0; i < keys.length; i++) {
			if (!isValid(keys[i])) {
				return false;
			}
		}
		return true;
	}

	/**
	 * 获取整型数
	 * 
	 * @param key
	 * @return
	 */
	public Integer getInt(K key) {
		return getInt(key,null);
	}

	/**
	 * 获取整型数
	 * 
	 * @param key
	 * @return
	 */
	public Integer getInt(K key, Integer defValue) {
		Number value = getNumber(key);
		return value != null ? value.intValue() : defValue;
	}

	public Long getLong(K key) {
		Number value = getNumber(key);
		return value != null ? value.longValue() : 0l;
	}

	public Double getDouble(K key) {
		Number value = getNumber(key);
		return value != null ? value.doubleValue() : 0d;
	}

	public boolean getBoolean(K key) {
		return getString(key).equals(Boolean.TRUE.toString());
	}

	public Number getNumber(K key) {
		Object value = get(key);
		if (value instanceof Number) {
			return (Number) value;
		} else {
			if (StringUtils.isValid(value))
				return new Double(StringUtils.asString(value));
			else
				return null;
		}
	}
	
	/**
	 * 取得日期
	 * @param key
	 * @return
	 */
	public Timestamp getDate(K key) {
		String date = getString(key);
		if(StringUtils.isValid(date)){
			return DateUtils.parse(date);
		}
		return null;
	}

	@Override
	public void clear() {
		this.map.clear();
	}

	@Override
	public boolean containsKey(Object key) {
		return this.map.containsKey(key);
	}

	@Override
	public boolean containsValue(Object value) {
		return this.map.containsKey(value);
	}

	@Override
	public Set<Map.Entry<K, V>> entrySet() {
		return this.map.entrySet();
	}

	@Override
	public boolean equals(Object obj) {
		return this.map.equals(obj);
	}

	@Override
	public V get(Object key) {
		return this.map.get(key);
	}

	@Override
	public int hashCode() {
		return this.map.hashCode();
	}
	@Override
	public boolean isEmpty() {
		return this.map.isEmpty();
	}
	@Override
	public Set<K> keySet() {
		return this.map.keySet();
	}
	@Override
	public V put(K key, V value) {
		return this.map.put(key, value);
	}
	@Override
	public void putAll(Map<? extends K, ? extends V> t) {
		this.map.putAll(t);
	}
	@Override
	public V remove(Object key) {
		return this.map.remove(key);
	}
	@Override
	public int size() {
		return this.map.size();
	}

	@Override
	public String toString() {
		return this.map.toString();
	}
	@Override
	public Collection<V> values() {
		return this.map.values();
	}
	
	/**
	 * 返回指定的键Map
	 * @param keys
	 * @return Map
	 */
	public Map<String,Object> getPropertiesMap(String... keys){
		Map<String,Object> map = new LinkedHashMap<String, Object>();
		for (String key : keys) {
			map.put(key, get(key));
		}
		return map;
	}

	public static void main(String args[]) {
		Object s = "";
		log.debug(String.valueOf(CharSequence.class.isInstance(s)));
	}
}
